package com.cskaoyan.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dto.UserLoginRequest;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.service.IUserLoginService;
import com.cskaoyan.user.vo.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author Bowen Deng
 * @since 2022/07/08 22:51
 */
@Service
public class UserLoginServiceImpl implements IUserLoginService {

    @Autowired
    MemberMapper memberMapper;

    /**
     * 用户登录，传入UserLoginRequest，返回UserLoginResponse
     * 需要处理异常
     *
     * @param request
     * @return com.cskaoyan.user.dto.UserLoginResponse
     * @author Bowen Deng
     * @since 2022/07/08 10:52 下午
     */
    @Override
    public UserLoginResponse login(UserLoginRequest request) {
        UserLoginResponse response = new UserLoginResponse();

        try {
            request.requestCheck();
            String userName = request.getUserName();
            String password = DigestUtils.md5DigestAsHex(request.getPassword().getBytes());
            Example example = new Example(Member.class);
            example.createCriteria()
                    .andEqualTo("username", userName)
                    .andEqualTo("password", password);
            List<Member> members = memberMapper.selectByExample(example);
            //密码错了
            if (CollectionUtils.isEmpty(members)) {
                response.setCode(UserRetCode.USERORPASSWORD_ERRROR.getCode());
                response.setMsg(UserRetCode.USERORPASSWORD_ERRROR.getMessage());
                return response;
            }
            Member member = members.get(0);
            //没激活
            if ("N".equals(member.getIsVerified())) {
                response.setCode(UserRetCode.USER_ISVERFIED_ERROR.getCode());
                response.setMsg(UserRetCode.USER_ISVERFIED_ERROR.getMessage());
                return response;
            }
            //我他妈真的是操了，学了converter，还是要手摇车
            response.setCode(UserRetCode.SUCCESS.getCode());
            response.setMsg(UserRetCode.SUCCESS.getMessage());
            //生成token
            TokenUser tokenUser = new TokenUser();
            tokenUser.setUid(member.getId());
            tokenUser.setUsername(member.getUsername());
            JwtTokenUtils jwtTokenUtils = JwtTokenUtils.builder().msg(JSON.toJSONString(tokenUser)).build();
            String token = jwtTokenUtils.creatJwtToken();
            response.setToken(token);

            response.setUsername(member.getUsername());
            response.setAddress(member.getAddress());
            response.setId(member.getId());
            response.setPhone(member.getPhone());
            response.setEmail(member.getEmail());
            response.setSex(member.getSex());
            response.setFile(member.getFile());
            response.setDescription(member.getDescription());
            response.setPoints(member.getPoints());
            //傻逼代码,balance没用，直接不要了
//        response.setBalance(member.getBalance());
            response.setState(member.getState());
        } catch (Exception e) {
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
